﻿using System;
using System.Collections.Generic;
using System.Data;
using OneSoft.DataAccess;

using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
namespace BySoft.Server.Business
{
    public partial class SysUserrole
    {
        public static DataTable GetRoleByUser(string userId)
        {
            try
            {
                return SysUserroleDA.GetRoleByUser(userId);
            }
            catch
            {
                throw;
            }
        }

        public static DataTable GetRoleByUserAndUserType(string userId)
        {
            try
            {
                return SysUserroleDA.GetRoleByUserAndUserType(userId);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取用户权限列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable GetRoleRightsByUser(string userId)
        {
            try
            {
                DataTable dtTemp = SysUserroleDA.GetRoleRightsByUser(userId);
                //DataTable dtGroup = SysWorkinggroupDA.GetWorkingGroupByUser(userId);
                //for(int i = 0;i<dtGroup.Rows.Count;i++)
                //{
                //   dtTemp.Merge(SysWorkinggroupDA.GetRoleRightsByGroup(dtGroup.Rows[i]["WORKINGGROUPID"].ToString()));
                //}
                DataTable dtReturn = dtTemp.Clone();
                for (int i = 0; i < dtTemp.Rows.Count; i++)
                {
                    DataRow[] drs = dtReturn.Select(" ModuleName = '" + dtTemp.Rows[i]["ModuleName"].ToString() + "'");
                    if (drs.Length == 0)
                    {
                        dtReturn.ImportRow(dtTemp.Rows[i]);
                    }
                    else
                    {
                        if (!(drs[0]["viewright"].ToString() == "无" && dtTemp.Rows[i]["viewright"].ToString() == "无"))
                        {
                            drs[0]["viewright"] = "有";
                        }
                        if (!(drs[0]["addright"].ToString() == "无" && dtTemp.Rows[i]["addright"].ToString() == "无"))
                        {
                            drs[0]["addright"] = "有";
                        }
                        if (!(drs[0]["updateright"].ToString() == "无" && dtTemp.Rows[i]["updateright"].ToString() == "无"))
                        {
                            drs[0]["updateright"] = "有";
                        }
                        if (!(drs[0]["deleteright"].ToString() == "无" && dtTemp.Rows[i]["deleteright"].ToString() == "无"))
                        {
                            drs[0]["deleteright"] = "有";
                        }
                        if (!(drs[0]["exportright"].ToString() == "无" && dtTemp.Rows[i]["exportright"].ToString() == "无"))
                        {
                            drs[0]["exportright"] = "有";
                        }
                        if (!String.IsNullOrEmpty(drs[0]["datars"].ToString()) && !String.IsNullOrEmpty(dtTemp.Rows[i]["datars"].ToString()))
                        {
                            if (int.Parse(drs[0]["datars"].ToString()) > int.Parse(dtTemp.Rows[i]["datars"].ToString()))
                            {
                                drs[0]["DataRights"] = dtTemp.Rows[i]["DataRights"].ToString();
                            }
                        }
                        if (String.IsNullOrEmpty(drs[0]["datars"].ToString()) && !String.IsNullOrEmpty(dtTemp.Rows[i]["datars"].ToString()))
                        {
                            drs[0]["DataRights"] = dtTemp.Rows[i]["DataRights"].ToString();
                        }

                    }
                }
                return dtReturn;
            }
            catch
            {
                throw;
            }
        }

        public static bool InsertSysUserrole(SysUserroleEntity objSysUserroleEntity, string sequenceName, string entityField)
        {
            if (objSysUserroleEntity == null || String.IsNullOrEmpty(sequenceName) || String.IsNullOrEmpty(entityField))
            {
                return false;
            }
            try
            {
                return DataAccessCommon.Insert(objSysUserroleEntity, sequenceName, entityField);
            }
            catch
            {
                throw;
            }

        }

        /// <summary>
        /// 获取用户权限(点击页面链接时)
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static DataTable GetRoleRightByUserAndUrl(string userId, string url)
        {
            DataTable dtReturn = SysUserroleDA.GetRoleRightByUserAndUrl(userId, url);

            if (dtReturn.Rows.Count > 1)
            {
                DataTable dtTemp = dtReturn.Clone();
                dtTemp.ImportRow(dtReturn.Rows[0]);
                for (int i = 1; i < dtReturn.Rows.Count; i++)
                {
                    if (!String.IsNullOrEmpty(dtReturn.Rows[i][SysRolerightsTable.C_DATARIGHTS].ToString().Trim()) && !String.IsNullOrEmpty(dtTemp.Rows[0][SysRolerightsTable.C_DATARIGHTS].ToString().Trim()))
                    {
                        if (int.Parse(dtReturn.Rows[i][SysRolerightsTable.C_DATARIGHTS].ToString().Trim()) < int.Parse(dtTemp.Rows[0][SysRolerightsTable.C_DATARIGHTS].ToString().Trim()))
                        {
                            dtTemp.Rows[0][SysRolerightsTable.C_DATARIGHTS] = dtReturn.Rows[i][SysRolerightsTable.C_DATARIGHTS].ToString();
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(dtReturn.Rows[i][SysRolerightsTable.C_DATARIGHTS].ToString().Trim()))
                        {
                            dtTemp.Rows[0][SysRolerightsTable.C_DATARIGHTS] = dtReturn.Rows[i][SysRolerightsTable.C_DATARIGHTS].ToString();
                        }
                    }
                    string sRight = "";
                    if (dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("R") || dtReturn.Rows[i][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("R"))
                    {
                        sRight += "R";
                    }
                    if (dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("C") || dtReturn.Rows[i][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("C"))
                    {
                        sRight += "C";
                    }
                    if (dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("D") || dtReturn.Rows[i][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("D"))
                    {
                        sRight += "D";
                    }
                    if (dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("U") || dtReturn.Rows[i][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("U"))
                    {
                        sRight += "U";
                    }
                    if (dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("E") || dtReturn.Rows[i][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("E"))
                    {
                        sRight += "E";
                    }
                    if (dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("A") || dtReturn.Rows[i][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("A"))
                    {
                        sRight += "A";
                    }
                    
                    dtTemp.Rows[0][SysRolerightsTable.C_RIGHTSCODE] = sRight;
                }
                return dtTemp;
            }
            else
            {
                return dtReturn;
            }
        }


        /// <summary>
        ///  判断用户是否是管理员
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static bool isAdmin(string userID)
        {
            List<SysUserroleEntity> listUserRoles = SysUserrole.GetByCondition("where " + SysUserroleTable.C_ROLEID + "='" + CommonConsts.Admin_Role_ID + "' and " + SysUserroleTable.C_USERID + "='" + userID.Replace("'", "''") + "'");
            if (listUserRoles.Count > 0)//----判断是否有管理员角色，管理员可设置状态
            {
                return true;
            }
            return false;
        }
    }
}
